package com.prs.crm.action.crm;

import java.io.File;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.crm.Rival;
import com.prs.crm.domain.crm.RivalLevel;
import com.prs.crm.domain.crm.RivalNature;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.report.ReportUtils;
import com.prs.crm.service.crm.RivalLevelService;
import com.prs.crm.service.crm.RivalNatureService;
import com.prs.crm.service.crm.RivalService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class RivalAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Rival rival;
	private RivalService rivalService;
	private PaginationSupport pagination;
	
	private RivalLevelService rivalLevelService;
	private RivalNatureService rivalNatureService;
	
	private Collection<RivalLevel> rivalLevels;
	private Collection<RivalNature> rivalNatures;
	
	private File upload;
	private String uploadFileName;
	private Integer[] ids;
	private String levelIds;
	
	private CodeCommon codeCommon;
	private CodeCommonService  codeCommonService;
	private String Encode;
	private Code code;
	private CodeService codeService;
	
	private Collection<CodeCommon> codelist;
	@Action("listRival")
	public String listRival() {
		return SUCCESS;
	}
	
	@Action("listRivalByLevel")
	public String listRivalByLevel(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Rival.class);
		criteria.addOrder(Order.desc("recordTime"));
		if(levelIds != null){
			if(!"all".endsWith(levelIds)){
				levelIds = ReportUtils.URLDecode(levelIds);
				levelIds = levelIds.split(",")[0];
				if(ToolKits.isInteger(levelIds)){
					criteria.createAlias("rivalLevel", "le");
					criteria.add(Restrictions.eq("le.id", Integer.parseInt(levelIds)));
				}
			}
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRivalService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("alterRival")
	public String alterRival() {
		return SUCCESS;
	}
	@Action("alterRivalByLevel")
	public String alterRivalByLevel(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Rival.class);
		criteria.addOrder(Order.desc("recordTime"));
		if(levelIds != null){
			if(!"all".endsWith(levelIds)){
				levelIds = ReportUtils.URLDecode(levelIds);
				levelIds = levelIds.split(",")[0];
				if(ToolKits.isInteger(levelIds)){
					criteria.createAlias("rivalLevel", "le");
					criteria.add(Restrictions.eq("le.id", Integer.parseInt(levelIds)));
				}
			}
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRivalService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("viewRival")
	public String viewRival() {
		return SUCCESS;
	}
	
	@Action("editRival")
	public String editRival() {
		this.codelist  =this.getCodeCommonService().getCodeCommonByCodeType("对手");
		rivalLevels = this.getRivalLevelService().getAllRivalLevel();
		rivalNatures = this.getRivalNatureService().getAllRivalNature();
		return SUCCESS;
	}
	
	@Clew(results = {"success","none"})
	@Action(value="saveRival",results={@Result(name="success",type="redirect",location="editRival"),
			@Result(name="none",type="redirect",location="alterRival"),
			@Result(name="error",type="chain",location="editRival")})
	public String saveRival() {
		if(rival.getName()==null||"".equals(rival.getName().trim())){
			addActionError("对手名称不能为空");
			return ERROR;
		}
		if(rival.getRivalcode()==null||rival.getRivalcode().trim().equals("")){
			addActionError("对手编号不能为空");
			return ERROR;
		}if(rival.getRivalLevel()==null){
			addActionError("威胁级别不能为空");
			return ERROR;
		}
		if(rival.getRivalNature()==null){
			addActionError("企业性质不能为空");
			return ERROR;
		}
		if(rival.getDistrict()==null){
			addActionError("所在地区不能为空");
			return ERROR;
			
		}
		
		

		if(rival.getId() == null) {
			
			if(this.getRivalService().existSameRival(rival.getName())) {
				this.addActionError("已录入相同名称的竞争对手");
				return ERROR;
			}
			
			if ("n".equals(Encode)) {
				int EncodeRplice = this.getCodeService().getBackRpliceAll("手动编码",
						"对手", rival.getRivalcode());
				if (EncodeRplice != 0) {
					this.addActionError("编码重复!请重新输入");
					return ERROR;
				}
			}
			if ("n".equals(Encode)) {
				Code codes = new Code();
				codes.setCodeValue(rival.getRivalcode());
				codes.setEntityName(this.getClass().getName());
				codes.setCodeType("手动编码");
				codes.setCodeMolde("对手");
				this.getCodeService().save(codes);
				rival.setCode(codes);
				
			} else {
				CodeCommon Vob = this.getCodeCommonService().getVobject(
						codeCommon.getId());
				Code codes = new Code();
				codes.setEntityName(this.getClass().getName());
				codes.setCodeValue(rival.getRivalcode());
				Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
				this.codeCommonService.saveOrUpdate(Vob);
				codes.setCodeType(Vob.getCodePlan());
				codes.setCodeMolde("对手");
				this.getCodeService().save(codes);
				rival.setCode(codes);

			}
			
			this.getRivalService().saveRival(rival, upload, uploadFileName);
			this.setPrompt("提交竞争对手成功");
			
			return SUCCESS;
		}
		else{
			
			this.getRivalService().saveRival(rival, upload, uploadFileName);
			this.setPrompt("变更竞争对手成功");

			return NONE;
		}
		
		
	}
	
	@Action(value="removeRival",results={@Result(name="success",type="redirect",location="alterRival"),
			@Result(name="error",type="chain",location="alterRival")})
	public String removeRival() {
		if(ids == null) {
			this.addActionError("请选择要删除的竞争对手");
			return ERROR;
		}
		this.getRivalService().batchRemove(ids);
		return SUCCESS;
	}
	
	public Rival getRival() {
		return rival;
	}
	public void setRival(Rival rival) {
		this.rival = rival;
	}
	public RivalService getRivalService() {
		return rivalService;
	}
	public void setRivalService(RivalService rivalService) {
		this.rivalService = rivalService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public File getUpload() {
		return upload;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	} 
	public String getUploadFileName() {
		return uploadFileName;
	}


	public void setRivalLevelService(RivalLevelService rivalLevelService) {
		this.rivalLevelService = rivalLevelService;
	}


	public RivalLevelService getRivalLevelService() {
		return rivalLevelService;
	}


	public void setRivalNatureService(RivalNatureService rivalNatureService) {
		this.rivalNatureService = rivalNatureService;
	}


	public RivalNatureService getRivalNatureService() {
		return rivalNatureService;
	}


	public void setRivalLevels(Collection<RivalLevel> rivalLevels) {
		this.rivalLevels = rivalLevels;
	}


	public Collection<RivalLevel> getRivalLevels() {
		return rivalLevels;
	}


	public void setRivalNatures(Collection<RivalNature> rivalNatures) {
		this.rivalNatures = rivalNatures;
	}


	public Collection<RivalNature> getRivalNatures() {
		return rivalNatures;
	}

	public String getLevelIds() {
		return levelIds;
	}

	public void setLevelIds(String levelIds) {
		this.levelIds = levelIds;
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}
	public static String Increase(String backupNum){
		String Inc="";
		int lengths=backupNum.length();
	    String lengthss=(Integer.parseInt(backupNum)+1)+"";
	    int increase=(lengthss.trim()).length();
	    if(lengths!=increase){
	    	String In="";
	    	for(int i=0;i<lengths-increase;i++){
	    		In=In+"0";
	    	}
	    	In=(In+lengthss).trim();
	    	Inc=In;
	    }else{
	    	Inc=lengthss;
	    }
	    return Inc.trim();
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}
	
}
